'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 叛军来袭
 * @author sundbcn@gmail.com (sun debing)
 */

var mongoose = require('mongoose');
var moment = require('moment');
var _ = require('lodash');
var rebelMgr = require('../mgr/rebelMgr');
var rebelItemSchema = require('./rebel/rebelItemSchema');
var rebelSkillSchema = require('./rebel/rebelSkillSchema');
var rebelConf = require('../config/rebelConf');

/**
 * 叛军来袭
 */
var rebelSchema = new mongoose.Schema({
    // 角色id
    role_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 已经通关的
    rebel_list: [Number],

    // 叛军技能列表
    rebel_skill_list: [rebelSkillSchema],

    // 今天通关的
    today_rebel_list: [Number],

    // 上次更新对手的时间
    last_refresh_time: {
        type: Date,
        default: 0
    }
});

rebelSchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 来袭技能
 * @param  {[type]}  rebel_id [description]
 * @return {Boolean}          [description]
 */
rebelSchema.methods.addRebelSkill = function (skill_type) {
    var use_times = 0;
    if (!this.getRebelSkill(skill_type)) {
        this.rebel_skill_list.push({
            skill_type: skill_type,
            use_times: 1
        });
        use_times = 1;
    }

    _.some(this.rebel_skill_list, function (rebel_item) {
        if (rebel_item.skill_type == skill_type) {
            rebel_item.addSkillUseTimes();
            use_times = rebel_item.use_times;
            return;
        }
    });
    return use_times;
};

/**
 * 获取来袭技能
 * @param  {[type]}  rebel_id [description]
 * @return {Boolean}          [description]
 */
rebelSchema.methods.getRebelSkill = function (skill_type) {
    for (var i = this.rebel_skill_list.length - 1; i >= 0; i--) {
        var rebel_item = this.rebel_skill_list[i];
        if (rebel_item.skill_type == skill_type) {
            return true;
        }
    }
    return false;
};

/**
 * 是否历史通关过来袭
 * @param  {[type]}  rebel_id [description]
 * @return {Boolean}          [description]
 */
rebelSchema.methods.isPassRebel = function (rebel_id) {
    return _.indexOf(this.rebel_list, rebel_id) !== -1;
};

/**
 * 是否已经已经通关过来袭
 * @param  {[type]}  rebel_id [description]
 * @return {Boolean}          [description]
 */
rebelSchema.methods.isTodayPassRebel = function (rebel_id) {
    return _.indexOf(this.today_rebel_list, rebel_id) !== -1;
};

/**
 * 是否前置通关了
 * @param  {[type]}  rebel_id [description]
 * @return {Boolean}          [description]
 */
rebelSchema.methods.isPassPreRebel = function (rebel_id) {
    var pre_rebel_id = rebelConf.getPreRebelId(rebel_id);
    if (pre_rebel_id === 0) {
        return true;
    }
    return this.isPassRebel(pre_rebel_id);
};

/**
 * 通关来袭
 * @param  {[type]} rebel_id [description]
 * @return {[type]}          [description]
 */
rebelSchema.methods.passRebel = function (rebel_id) {
    if (!this.isPassRebel(rebel_id)) {
        this.rebel_list.push(rebel_id);
    }

    if (!!this.isTodayPassRebel(rebel_id)) {
        return false;
    }
    this.today_rebel_list.push(rebel_id);
    return true;
};

/**
 * 获取上次刷新对手的时间
 * @return {[type]} [description]
 */
rebelSchema.methods.getLastRefreshTime = function () {
    return this.last_refresh_time;
};

/**
 * 刷新当天通过的来袭
 * @return {[type]} [description]
 */
rebelSchema.methods.refreshTodayPassRebel = function () {
    this.last_refresh_time = moment();
    this.today_rebel_list = [];
};

/**
 * 获取修改
 * @return {[type]} [description]
 */
rebelSchema.methods.getLastChange = function () {
    return this.toJSON();
};

/**
 * 推送
 * @return {[type]} [description]
 */
rebelSchema.methods.push = function () {
    this.pushModify();
};

/**
 * 推送修改
 * @return {[type]} [description]
 */
rebelSchema.methods.pushModify = function () {
    rebelMgr.sendRebelChanged(
        this.role_id,
        this.getLastChange(),
        function (err, fails) {
            if (err) {
                console.error('send rebel message error: %j, fail ids: %j', err, fails);
                return;
            }
        });
};

if (!rebelSchema.options.toJSON) {
    rebelSchema.options.toJSON = {};
}
/* jshint unused:false */
rebelSchema.options.toJSON.transform = function (doc, ret) {
    delete ret.role_id;
    delete ret._id;
    delete ret.__v;
};

mongoose.model('Rebel', rebelSchema);
